﻿using Dapper;
using Domain.Entity.Message;
using Domain.Entity.System_setting;
using Domain.IRepository.System_setting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace EFCore.Repository.System_setting
{
    [Dependency(ServiceLifetime.Scoped)]
    public class TreeRepository : ITreeRepository
    {
        private MyDbContext myDbContext { get; set; }

        public TreeRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public List<Tree> GetTrees(int id)
        {
            string sql = $@"SELECT R.Role_id, Role_name FROM dbo.Ment_role R, dbo.Employee_role ER
            WHERE R.Role_id=ER.Role_id AND Employee_id={id} AND Is_del=0";
            List<Ment_role> list = myDbContext.Database.GetDbConnection().Query<Ment_role>(sql).ToList();
            List<Tree> list2 = new List<Tree>();
            foreach (Ment_role role in list)
            {
                Ment_role role1 = new Ment_role()
                {
                    Role_id = role.Role_id,
                    Role_name = role.Role_name
                };
                sql = $@"select M.Menu_id, Menu_name, Menu_pid, [Path], Menu_icon, Order_num, Create_by FROM dbo.Ment_menu M
                INNER JOIN dbo.Role_menu RM ON M.Menu_id=RM.Menu_id
                WHERE RM.Role_id=3 AND M.Is_del=0";
                List<Ment_menu> menu = myDbContext.Database.GetDbConnection().Query<Ment_menu>(sql).ToList();
                List<Tree> sys_Trees = new List<Tree>();
                foreach (Ment_menu menuitem in menu)
                {
                    Tree tree = new Tree()
                    {
                        Id = menuitem.Menu_id,
                        Treename = menuitem.Menu_name,
                        Ordernum = menuitem.Order_num,
                        AuthName = menuitem.Menu_name,
                        Pid = menuitem.Menu_pid,
                        Path = menuitem.Path,
                        Icon = menuitem.Menu_icon,
                    };
                    sys_Trees.Add(tree);
                }
                list2 = GetChildTrees(sys_Trees, 0);
            }
            return list2;
        }

        public List<Tree> GetChildTrees(List<Tree> list, int pid)
        {
            List<Tree> list2 = new List<Tree>();
            var trees = list.Where(e => e.Pid == pid);
            foreach (var tree in trees)
            {
                Tree sys_Tree = new Tree()
                {
                    Id = tree.Id,
                    Treename = tree.Treename,
                    Ordernum = tree.Ordernum,
                    AuthName = tree.AuthName,
                    Pid = tree.Pid,
                    Path = tree.Path,
                    Icon = tree.Icon,
                };
                if (GetChildTrees(list, tree.Id).Count != 0)
                {
                    sys_Tree.Children = GetChildTrees(list, tree.Id);
                }
                list2.Add(sys_Tree);
            }
            return list2;

        }
    }
}
